home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / chown.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  3KB  |  106 lines

  1. /*
  2.  * chown-    Change the owner and/or group ID of a file.
  3.  *
  4.  * Author:    Bert Reuling (bert@kyber.UUCP) 
  5.  *
  6.  * Revision History:
  7.  *    1.1  89/09/25 bert@kyber.UUCP         Initial revision
  8.  *         89/10/04 waltje@kyber.UUCP        Adapted to MINIX Style Sheet
  9.  */
  10. #include <sys/types.h>
  11. #include <ctype.h>
  12. #include <grp.h>
  13. #include <pwd.h>
  14. #include <string.h>
  15. #include <sys/stat.h>
  16. #include <stdio.h>
  17.  
  18. #ifndef TRUE
  19. #    define TRUE  1
  20. #    define FALSE 0
  21. #endif
  22.  
  23. static char rcsid[] = "$Header: chown.c,v 1.1 89/10/04 20:45:00 bert Exp $";
  24.  
  25.  
  26. main(argc, argv)
  27. int argc;
  28. char *argv[];
  29. {
  30.   int i, uid, gid, Chown, status = 0;
  31.   char *pgmname, *uids, *gids;
  32.   struct passwd *pwd;
  33.   struct group *grp;
  34.   struct stat st;
  35.  
  36.   if ((pgmname = strrchr(argv[0], '/')) != NULL) pgmname++;
  37.     else pgmname = argv[0];
  38.   if (strcmp(pgmname, "chown") == 0) Chown = TRUE;
  39.     else if (strcmp(pgmname, "chgrp") == 0) Chown = FALSE;
  40.            else {
  41.                (void) fprintf(stderr, "%s: should be named \"chown\" or \"chgrp\"\n", argv[0]);
  42.              (void) exit(-1);
  43.           }
  44.   if (argc < 3) {
  45.     (void) fprintf(stderr,"Usage: %s %s file ...\n", pgmname, (Chown) ? "owner" : "group");
  46.           (void) exit(1);
  47.   }
  48.   if ((gids = strchr(argv[1], '.')) != NULL) {
  49.       *gids++ = '\0';
  50.           uids = argv[1];
  51.   } else {
  52.           if (Chown) {
  53.              uids = argv[1];
  54.              gids = NULL;
  55.             } else {
  56.                uids = NULL;
  57.                gids = argv[1];
  58.               }
  59.   }
  60.   if (uids == NULL) pwd = NULL;
  61.     else {
  62.           if (isdigit(*uids)) pwd = getpwuid(atoi(uids));
  63.               else pwd = getpwnam(uids);
  64.           if (pwd == NULL) {
  65.              (void) fprintf(stderr, "%s: unknown user id %s\n", pgmname, uids);
  66.              (void) exit(-1);
  67.             }
  68.     }
  69.   if (gids == NULL) grp = NULL;
  70.     else {
  71.           if (isdigit(*gids)) grp = getgrgid(atoi(gids));
  72.             else grp = getgrnam(gids);
  73.           if (grp == NULL) {
  74.              (void) fprintf(stderr, "%s: unknown group: %s\n", pgmname, gids);
  75.              (void) exit(-1);
  76.             }
  77.     }
  78.   for (i = 2; i < argc; i++) {
  79.       if (stat(argv[i], &st) != -1) {
  80.         uid = (pwd == NULL) ? st.st_uid : pwd->pw_uid;
  81.         gid = (grp == NULL) ? st.st_gid : grp->gr_gid;
  82.         if (chown(argv[i], uid, gid) == -1) {
  83.             (void) fprintf(stderr,"%s: not changed\n", argv[i]);
  84.                 status++;
  85.         }
  86. #ifdef _MINIX
  87.         /*
  88.          * chown(2) should do this ...
  89.          */
  90.         if (getuid() != 0) {
  91.         st.st_mode &= ~S_ISUID;
  92.                 st.st_mode &= ~S_ISGID;
  93.                 if (chmod(argv[i], st.st_mode) == -1) {
  94.                        (void) fprintf(stderr, "%s: mode not changed\n", argv[i]);
  95.                        status++;
  96.                 }
  97.         }
  98. #endif
  99.      } else {
  100.              (void) perror(argv[i]);
  101.              status++;
  102.        }
  103.   }
  104.   (void) exit(status);
  105. }
  106.